package com.jalarbee.core.util;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

public class DateUtils {

	// public static final TimeZone SYSTEM_TIMEZONE = TimeZone.getDefault();
	// public static final TimeZone UTC_TIMEZONE = TimeZone.getTimeZone("UTC");

	public static Date getDateBeforeEndOfMonth(Date date) {
		return DateUtils.today();
	}

	public static Date today() {
		return new Date();
	}

	public static Date tomorrow() {
		return dayAfter(new Date());
	}

	public static Date yesterday() {
		return dayBefore(new Date());
	}

	public static Date dayAfter(Date date) {
		Calendar calendar = getCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_YEAR, 1);
		return calendar.getTime();
	}

	public static Date dayBefore(Date date) {
		Calendar calendar = getCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_YEAR, -1);
		return calendar.getTime();
	}

	public static Date monthAfter(Date date) {
		Calendar calendar = getCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, 1);
		resetDayOfMonth(calendar);
		resetTime(calendar);
		return calendar.getTime();
	}

	public static Date monthBefore(Date date) {
		Calendar calendar = getCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.MONTH, -1);
		resetDayOfMonth(calendar);
		resetTime(calendar);
		return calendar.getTime();
	}

	public static Date nextMonth() {
		return monthAfter(new Date());
	}

	public static Date lastMonth() {
		return monthAfter(new Date());
	}

	public static Date thisMonth() {
		return new Date();
	}

	public static Date getLastDateOfMonth(Date date) {
		return getLastDayOfMonthWithOffset(date, 0);
	}

	private static void resetTime(Calendar calendar) {
		calendar.set(Calendar.HOUR, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
	}

	private static void resetDayOfMonth(Calendar calendar) {
		calendar.set(Calendar.DAY_OF_MONTH, 1);
	}

	private static Calendar getCalendar() {
		return new GregorianCalendar();
	}

	@SuppressWarnings("unused")
	private static void resetDayOfWeek(Calendar calendar) {
		calendar.set(Calendar.DAY_OF_WEEK, 1);
	}

	public static Date getLastDayOfMonthWithOffset(Date date, int days) {
		Calendar calendar = getCalendar();
		calendar.setTime(date);
		calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.DAY_OF_MONTH, -days - 1);
		return calendar.getTime();
	}

}
